package com.paas.kd.controller;

import com.paas.kd.config.ResourceConfig;
import com.paas.kd.util.ResponseData;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.EspServiceCode;
import com.sf.csim.express.service.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author : xuzhennan
 * @Project : kdtong
 * @Package : com.paas.kd.controller
 * @email : xuzhennan@58.com
 * @date : 2021年09月04日 下午3:45
 */
@RestController
@RequestMapping("/sf")
public class SfLassController {


    @Autowired
    public ResourceConfig resourceConfig;

    @RequestMapping("/makeOrder")
    // 下单接口说明链接
    // https://open.sf-express.com/#/Api/expressApi/645842?level3=819535
    public ResponseData makeOrder() {
        //此处替换为您在丰桥平台获取的顾客编码
        String CLIENT_CODE = resourceConfig.getClientCode();
        //此处替换为您在丰桥平台获取的校验码
        String CHECK_WORD = resourceConfig.getCleckCode();
        String CALL_URL_BOX = resourceConfig.getBoxUrl();
        EspServiceCode testService = EspServiceCode.EXP_RECE_CREATE_ORDER; //下订单
        // CallExpressServiceTools client = CallExpressServiceTools.getInstance();
        String timeStamp = String.valueOf(System.currentTimeMillis());
        String msgData = "{\n" +
                "\t\"cargoDetails\": [{\n" +
                "\t\t\"amount\": 308.0,\n" +
                "\t\t\"count\": 1.0,\n" +
                "\t\t\"name\": \"君宝牌地毯\",\n" +
                "\t\t\"unit\": \"个\",\n" +
                "\t\t\"volume\": 0.0,\n" +
                "\t\t\"weight\": 0.1\n" +
                "\t}],\n" +
                "\t\"contactInfoList\": [{\n" +
                "\t\t\"address\": \"十堰市丹江口市公园路155号\",\n" +
                "\t\t\"city\": \"十堰市\",\n" +
                "\t\t\"company\": \"清雅轩保健品专营店\",\n" +
                "\t\t\"contact\": \"张三丰\",\n" +
                "\t\t\"contactType\": 1,\n" +
                "\t\t\"county\": \"武当山风景区\",\n" +
                "\t\t\"mobile\": \"17006805888\",\n" +
                "\t\t\"province\": \"湖北省\"\n" +
                "\t}, {\n" +
                "\t\t\"address\": \"湖北省襄阳市襄城区环城东路122号\",\n" +
                "\t\t\"city\": \"襄阳市\",\n" +
                "\t\t\"contact\": \"郭襄阳\",\n" +
                "\t\t\"county\": \"襄城区\",\n" +
                "\t\t\"contactType\": 2,\n" +
                "\t\t\"mobile\": \"18963828829\",\n" +
                "\t\t\"province\": \"湖北省\"\n" +
                "\t}],\n" +
                "\t\"customsInfo\": {},\n" +
                "\t\"expressTypeId\": 1,\n" +
                "\t\"extraInfoList\": [],\n" +
                "\t\"isOneselfPickup\": 0,\n" +
                "\t\"language\": \"zh-CN\",\n" +
                "\t\"monthlyCard\": \"7551234567\",\n" +
                "\t\"orderId\": \"QIAO-20200618-003\",\n" +
                "\t\"parcelQty\": 1,\n" +
                "\t\"payMethod\": 1,\n" +
                "\t\"totalWeight\": 6,\n" +
                "}";
        long startTime = System.currentTimeMillis();
        String result = http(testService.getCode(), msgData, CLIENT_CODE, timeStamp, CHECK_WORD, CALL_URL_BOX);
        System.out.println("====调用丰桥的接口服务代码：" + String.valueOf(testService.getCode()) + " 接口耗时：" + String.valueOf(System.currentTimeMillis() - startTime) + "====");
        System.out.println("===调用地址 ===" + CALL_URL_BOX);
        System.out.println("===顾客编码 ===" + CLIENT_CODE);
        System.out.println("===返回结果：" + result);
        return ResponseData.buildSuccess(result);
    }

    @RequestMapping("/orderOff")
    // 确认或取消订单接口说明链接
    // http://localhost:8081/sf/orderOff
    public ResponseData orderOff() {
        String CLIENT_CODE = resourceConfig.getClientCode();
        String CHECK_WORD = resourceConfig.getCleckCode();
        String CALL_URL_BOX = resourceConfig.getBoxUrl();
        // 订单取消
        EspServiceCode testService = EspServiceCode.EXP_RECE_UPDATE_ORDER;

        String timeStamp = String.valueOf(System.currentTimeMillis());
        // 订单确认
//        String msgData = "{\n" +
//                "    \"dealType\": 1,\n" +
//                "    \"orderId\": \"QIAO-20200618-003\",\n" +
//                "    \"totalHeight\": 29.98,\n" +
//                "    \"totalLength\": 29.98,\n" +
//                "    \"totalVolume\": 26946.035992000005,\n" +
//                "    \"totalWeight\": 2.09,\n" +
//                "    \"totalWidth\": 29.98,\n" +
//                "    \"waybillNoInfoList\": [{\n" +
//                "        \"waybillNo\": \"SF2000090670189\",\n" +
//                "        \"waybillType\": 1\n" +
//                "    }]\n" +
//                "}";
        // 下单接口和取消订单接口的orderId一定要一致 才能成功测试取消订单
        String msgData = "{\n" +
                "\t\"confirmType\": 3,\n" +
                "\t\"dealType\": 2,\n" +
                "\t\"language\": \"zh-CN\",\n" +
                "\t\"orderId\": \"QIAO-20200618-003\",\n" +
                "\t\"totalWeight\": 1,\n" +
                "\t\"waybillNoInfoList\": []\n" +
                "}";
        // 顾客编码 ，对应丰桥上获取的clientCode
        String result = http(testService.getCode(), msgData, CLIENT_CODE, timeStamp, CHECK_WORD, CALL_URL_BOX);
        System.out.println("===调用地址 ===" + CALL_URL_BOX);
        System.out.println("===顾客编码 ===" + CLIENT_CODE);
        System.out.println("===返回结果：" + result);
        return ResponseData.buildSuccess(result);
    }

    // 查询路由说明链接
    // https://open.sf-express.com/#/Api/expressApi/645842?level3=221529
    @RequestMapping("/searchRoute")
    public ResponseData searchRoute() {
        String CLIENT_CODE = resourceConfig.getClientCode();
        String CHECK_WORD = resourceConfig.getCleckCode();
        String CALL_URL_BOX = resourceConfig.getBoxUrl();
        // 订单取消
        EspServiceCode testService = EspServiceCode.EXP_RECE_UPDATE_ORDER;

        String timeStamp = String.valueOf(System.currentTimeMillis());
        // 下单接口和取消订单接口的orderId一定要一致 才能成功测试取消订单
        String msgData = "{\n" +
                "\t\"Body\": {\n" +
                "\t\t\"WaybillRoute\": [{\n" +
                "\t\t\t\"mailno\": \"SF7444400031887\",\n" +
                "\t\t\t\"acceptAddress\": \"test\",\n" +
                "\t\t\t\"reasonName\": \"\",\n" +
                "\t\t\t\"orderid\": \"QIAO-20200618-003\",\n" +
                "\t\t\t\"acceptTime\": \"2020-05-11 16:56:54\",\n" +
                "\t\t\t\"remark\": \"test\",\n" +
                "\t\t\t\"opCode\": \"50\",\n" +
                "\t\t\t\"id\": \"158918741444476\",\n" +
                "\t\t\t\"reasonCode\": \"\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"mailno\": \"SF7444400031887\",\n" +
                "\t\t\t\"acceptAddress\": \"test\",\n" +
                "\t\t\t\"reasonName\": \"\",\n" +
                "\t\t\t\"orderid\": \"QIAO-20200618-003\",\n" +
                "\t\t\t\"acceptTime\": \"2020-05-11 16:56:54\",\n" +
                "\t\t\t\"remark\": \"test\",\n" +
                "\t\t\t\"opCode\": \"80\",\n" +
                "\t\t\t\"id\": \"158918741457126\",\n" +
                "\t\t\t\"reasonCode\": \"\"\n" +
                "\t\t}]\n" +
                "\t}\n" +
                "}";
        // 顾客编码 ，对应丰桥上获取的clientCode
        String result = http(testService.getCode(), msgData, CLIENT_CODE, timeStamp, CHECK_WORD, CALL_URL_BOX);
        System.out.println("===调用地址 ===" + CALL_URL_BOX);
        System.out.println("===顾客编码 ===" + CLIENT_CODE);
        System.out.println("===返回结果：" + result);
        return ResponseData.buildSuccess(result);
    }


    private String http(String serviceCode, String msgData, String clientCode, String timeStamp, String checkCode, String callUrl) {
        String timestamp = System.currentTimeMillis() + "";
        // 业务数据报文
        Map<String, String> params = new HashMap<String, String>();
        params.put("partnerID", clientCode);
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        // 接口服务码
        params.put("serviceCode", serviceCode);
        params.put("timestamp", timeStamp);
        params.put("msgData", msgData);
        try {
            params.put("msgDigest", CallExpressServiceTools.getMsgDigest(msgData, timeStamp, checkCode));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        long startTime = System.currentTimeMillis();
        System.out.println("====调用请求：" + params.get("msgData"));
        String result = null;
        try {
            result = HttpClientUtil.post(callUrl, params);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

}
